package classes.io;

import org.junit.Test;

import java.io.*;

/**
 * 文件字节流
 */
public class FileInputOutputStreamTest {
    @Test
    public void test() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream("./src/classes/io/book2.pdf");  //打开文件流
            fileOutputStream = new FileOutputStream("./src/classes/io/book.pdf");
            byte[] buf = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buf)) != -1) {
                fileOutputStream.write(buf, 0, len); // 把buf写入输出流当中
            }
        } catch (IOException e) {  // 文件找不到的异常
            e.printStackTrace();
        } finally { //无论如何都要关掉文件
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
